<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>农产品溯源系统用户注册</title>
    <script src="/js/jquery-3.7.1.min.js"></script>
    <style>
       body {
        font-family: 'Arial', sans-serif;
        background: url('../static/img.png') no-repeat center center fixed !important; /* Replace with your image URL */
        background-size: cover; /* Ensures the image covers the whole page */
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
}

        .content {
            background-color: rgba(255, 255, 255, 0.8);
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        .header {
            text-align: center;
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        .formItem {
            margin-bottom: 15px;
        }
        .formItem label {
            display: block;
            margin-bottom: 5px;
            color: #666;
        }
        .formItem input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btnBox {
            text-align: center;
        }
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn.primary {
            background-color: #28a745;
        }
        .btn.primary:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>

<div class="content">
    <div class="header">
        <div class="headerItem">农产品溯源系统用户注册</div>
    </div>
    <div class="coBodyItem">
        <div class="formItem"><label>用户名:</label> <input id="username" name="username" placeholder="请输入用户名" required /></div>
        <div class="formItem"><label>密码:</label> <input id="password" name="password" type="password" placeholder="请输入密码" required /></div>
        <div class="formItem"><label>昵称:</label> <input id="nickname" name="nickname" placeholder="请输入昵称" required /></div>
        <div class="btnBox">
            <button class="btn primary" onclick="handleRegister()">注册</button>
        </div>
    </div>
</div>

<script>
    function handleRegister() {
        var user = {
            username: $('#username').val(),
            password: $('#password').val(),
            nickname: $('#nickname').val()
        };

        $.ajax({
            url: 'http://localhost:8007/user/register',  // 后端接口地址
            type: 'POST',
            contentType: 'application/json',
            data: JSON.stringify(user),
            success: function(response) {
                if (response.status === 'success') {
                    alert('注册成功');
                    window.location.href = 'http://localhost:8007/user/login';  // 跳转到登录页面
                } else {
                    alert('注册失败: ' + response.message);
                }
            },
            error: function(xhr, status, error) {
                alert('注册请求失败');
                console.error(error);
            }
        });
    }
</script>

</body>
</html>
